package com.zjff.mobiletwo.service;

import com.taobao.api.internal.util.StringUtils;
import com.zjff.mobiletwo.dao.UserDao;
import com.zjff.mobiletwo.entity.ResultEntity;
import com.zjff.mobiletwo.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

@Service
public class UpdateServiceImpl implements UpdateService {
    @Autowired
    UserDao userDao;
    @Override
    public UserEntity getUserByUsername(String username) {
        UserEntity user = userDao.getUserByUsername(username);
        return user;
    }
    @Override
    public String updatePassword(String username, String password, String newPsw,String conPaw) {
        //根据username获取user
        UserEntity user = userDao.getUserByPhone(username);
        if (user != null){
            if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(newPsw) ||StringUtils.isEmpty(conPaw)){
                System.out.println("输入不能为空");
                return ResultEntity.Fail(-10007,"输入不能为空!");
            }else if (!user.getPassword().equals(password)){
                System.out.println("原密码错误");
                return ResultEntity.Fail(-10007,"原密码错误!");
            }else if (user.getPassword().equals(newPsw)){
                System.out.println("新密码不能与旧密码相同！");
                return ResultEntity.Fail(-10007,"新密码不能与旧密码相同!");
            } else if(!newPsw.equals(conPaw)){
                System.out.println("两次密码输入不同！");
                return ResultEntity.Fail(-10007,"两次密码输入不同!");
            }else if (user.getPassword().equals(password)){
                userDao.updatePasswordByUsername(username,newPsw);
                System.out.println("密码修改成功");
                return ResultEntity.Success(null,userDao.updatePasswordByUsername(username,newPsw),0,"修改密码成功!");
            }else{
                System.out.println("用户密码未修改");
                return "用户密码未修改";
            }
        }else{
            System.out.println("用户不能为空");
            return ResultEntity.Fail(-10007,"用户不能为空!");
        }
    }

    @Override
    public String resetPassword(String username, String password, String conPaw) {
        //根据username获取user
        UserEntity user = userDao.getUserByPhone(username);
        if (user != null){
            if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(conPaw)){
                System.out.println("输入不能为空");
                return ResultEntity.Fail(-10007,"输入不能为空!");
            } else if (!password.equals(conPaw)) {
                System.out.println("两次密码输入不同！");
                return ResultEntity.Fail(-10007,"两次密码输入不同!");
            }else{
                userDao.updatePasswordByUsername(username,password);
                System.out.println("密码修改成功");
                return ResultEntity.Success(null,userDao.updatePasswordByUsername(username,password),0,"获取成功!");
            }
        }else{
            System.out.println("用户不能为空");
            return ResultEntity.Fail(-10007,"用户不能为空!");
        }
    }

}
